package Tools;

import InterMessages.SunspotsDeployStatus;

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * User: leontiad
 * Date: Mar 9, 2010
 * Time: 1:33:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class ExecuterGUI implements Executer {

    public void executeCommand(String input, File fdir, SunspotsDeployStatus m_sunspotsDeployStatus, final JTextArea displayScreen, BlockingQueue bq) {

        DeployConsoleTask(input, fdir, m_sunspotsDeployStatus, displayScreen, bq);
        return;


    }

    private String DeployConsoleTask(String input, File fdir, SunspotsDeployStatus m_sunspotsDeployStatus, final JTextArea displayScreen, BlockingQueue bq) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                displayScreen.append("\nWaiting...");
            }
        });

        Process p = null;
        ProcessBuilder builder;

        System.out.print(SwingUtilities.isEventDispatchThread());
        ArrayList<String> command = new ArrayList<String>();

        if (System.getProperty("os.name").toLowerCase().contains("windows")) {

            command.add("cmd");
            command.add("/" + System.getenv("SystemDrive").substring(0, 1));

        } else {//Linux,Solaris,Ubuntu,Mac are using the bash
            command.add("/bin/sh");
            command.add("-c");
        }

        command.add(input);
        builder = new ProcessBuilder(command);
        builder.directory(fdir);

        try {
            p = builder.start();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        String launchHeaderString =
                "\n***************************************************\n" +
                        "      Deploying from Directory " + fdir.toString() + "\n" +
                        "***************************************************\n";

        //m_sunspotsDeployStatus.deployStatus.add(launchHeaderString);
        displayScreen.append(launchHeaderString);
        //displayScreen.append(launchHeaderString + "\n");
        assert p != null;
        StreamGobblerGUI errorgobler = new StreamGobblerGUI(p.getErrorStream(), "ERROR", bq);
        StreamGobblerGUI outputGobler = new StreamGobblerGUI(p.getInputStream(), "output", bq);
        //       MonitorThread mon = new MonitorThread(bq, displayScreen);
        errorgobler.start();
        outputGobler.start();

        try {
            errorgobler.join();
            outputGobler.join();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //mon.run();
        System.out.println("error:" + errorgobler.getResult());
        System.out.println("out:" + outputGobler.getResult());

        displayScreen.append(errorgobler.getResult() + "\n");
        displayScreen.append(outputGobler.getResult() + "\n");

        return (errorgobler.getResult() + outputGobler.getResult());
        // return (errorgobler.getResult() == "") ? false : true;
    }

}
